package com.es.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.client.Node;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @email: fahoud@163.com
 * @author: duzifeng
 * @date: 2023/6/1
 * @time: 9:51
 * Description:
 */
@Configuration
public class EsConfig {
    private final String host;
    private final Integer port;
    /**
     * ms
     */

    private final Integer connectTimeout;
    private final Integer socketTimeout;
    private final Integer connectionRequestTimeout;
    private final RestClient restClient;
    private final ElasticsearchTransport elasticsearchTransport;
    private final ElasticsearchClient elasticsearchClient;
    public EsConfig(
            @Value("${elasticsearch.host}") String host,
            @Value("${elasticsearch.port}") Integer port,
            @Value("${elasticsearch.connectTimeout:10000}") Integer connectTimeout,
            @Value("${elasticsearch.socketTimeout:15000}") Integer socketTimeout,
            @Value("${elasticsearch.connectionRequestTimeout:500}") Integer connectionRequestTimeout
    ) {
        this.host = host;
        this.port = port;
        this.connectTimeout = connectTimeout;
        this.socketTimeout = socketTimeout;
        this.connectionRequestTimeout = connectionRequestTimeout;
        restClient = RestClient.builder(new HttpHost(host, port))
                .setRequestConfigCallback(
                        requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(connectTimeout)
                                .setConnectionRequestTimeout(connectionRequestTimeout)
                                .setSocketTimeout(socketTimeout)
                )
                .setHttpClientConfigCallback(
                        httpClientBuilder -> httpClientBuilder.setDefaultIOReactorConfig(
                                IOReactorConfig.custom()
                                        .setIoThreadCount(2)
                                        .build()
                        )
                )
                .build();
        elasticsearchTransport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        elasticsearchClient = new ElasticsearchClient(elasticsearchTransport);
    }

    @Bean
    public RestClient getRestClient() {
        return restClient;
    }

    @Bean
    public ElasticsearchTransport getElasticsearchTransport() {
        return elasticsearchTransport;
    }

    @Bean
    public ElasticsearchClient getElasticsearchClient() {
        return elasticsearchClient;
    }
}
